<template>
	<view class="white">
		<u-navbar :is-back="false" :border-bottom="false" :background="{ background: '#fff' }">
			<view class="slot u-flex align-center justify-center">
				<view class="cuIcon-back" @click="Back()"></view>
				|
				<navigator url="/pages/index/index" open-type="switchTab" hover-class="other-navigator-hover">
					<view class="cuIcon-home"></view>
				</navigator>
			</view>
		</u-navbar>
		<view class="main">
			<view class="swiper">
				<swiper indicator-active-color="#fff" :autoplay="autoplay" :interval="2000" :duration="500" @change="change"
					style="height: 400rpx;">
						<swiper-item  v-for="(item,i) in get_img" :key="i">
							<view class="banner">
								<image :src="item" mode="aspectFill"></image>
							</view>
						</swiper-item>
				</swiper>
			</view>
			<!-- <image src="../../static/images/gongshi-img.png" mode="aspectFill"></image> -->
			
			<view class="u-flex justify-between text">
				<view class="">
					<view class="name text-bold">
						{{detail.name||''}}
					</view>
					<view class="u-font-24">
						{{detail.views||0}}人浏览
					</view>
				</view>

			</view>
			<view class="u-flex justify-between fenxian">
				<view class="u-font-36 text-orange">
					{{detail.free_intro||''}}
				</view>
				<image src="../../static/images/xiangzhi.png" mode="aspectFill" @tap="showModal" data-target="Modal"></image>
			</view>
			<view class="img" v-if="user_record && user_record.length>0">
				<view class="u-font-30">
					他们都在逛
				</view>
				<view class="u-flex justify-between ">
					<view class="avatar">
						<image :src="item.avatar" mode="aspectFill" v-for="(item,index) in user_record" :key="index" v-if="index<6"></image>
						<image src="../../static/images/shenl.png" mode="aspectFill" v-if="user_record.length>5"></image>
					</view>
					<button class="cu-btn round bg-orange" open-type="share">
						分享
					</button>
				</view>
			</view>

		</view>
		<u-gap height="20" bg-color="#F1F1F1"></u-gap>
		<view class="tabs">
			<u-tabs :list="list" :is-scroll="false" :current="current" :bold="false" active-color="#FE7E00"
				@change="change"></u-tabs>
		</view>
		<u-gap height="20" bg-color="#F1F1F1"></u-gap>
		<view class="u-content" v-if="!current">
			<u-parse :html="detail.content" :tag-style="style"></u-parse>
		</view>
		<view class="u-content nulu" v-else>
			<block v-if="is_show_course==1">
				<view class="u-flex justify-between">
					<view class="text-bold u-font-30">
						全部内容({{course_total}})
					</view>
					<view class="">
						<image src="../../static/images/daoxv.png" mode="aspectFit" @click="handleSort()"></image>
					</view>
				</view>
				<view class="shiping u-flex justify-between" v-for="(item,index) in course_list" :key="index">
				<view class="flex"  @click="handleLearnUnit(item)">
					<view class="cu-btn round sm">
						{{item.type_name}}
					</view>
					<view class="">
						<view class="u-font-30">
							{{item.name}}
						</view>
						<view class="u-font-24">
							{{item.other_intro}}
						</view>
					</view>
				</view>
				<view class="xiangzhi">
					<!-- 有用的地方，给来下载按钮的 -->
					<image src="../../static/images/xiangzhi.png" mode="aspectFill" @click="handleDownItem(item)"  v-if="is_exist==1"></image>
					<!-- 90% -->
				</view>
			</view>
			</block>
		</view>
		
		<u-gap height="110"></u-gap>
		<view class="u-flex justify-center bottom" v-if="initLoading">
			<view class="text-center">
				<view class=" u-font-40" :class="collect?'cuIcon-favorfill text-orange':'cuIcon-favor'" @click="djqd"></view>
				<view class="u-font-24">
					收藏
				</view>
			</view>
			
			<button v-if="is_exist==1" class="cu-btn u-font-30 bg-orange round" @tap="handleLearn" data-target="bottomModal">
				立即学习
			</button>
			<button v-else-if="get_free_state" class="cu-btn u-font-30 bg-orange round" @tap="showModal" data-target="bottomModal">
				免费领取
			</button>
			<button v-else class="cu-btn u-font-30 bg-orange round" @click="handleBuy" :loading="buySubmitLoading">
				<text class="cuIcon-lock u-font-30" style="margin-right: 10rpx;"></text>立即购买
			</button>
		</view>
		
		
		<view class="cu-modal bottom-modal" :class="modalName=='bottomModal'?'show':''">
			<view class="cu-dialog">
				<view class="">
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close u-font-40"></text>
					</view>
					<view class="u-font-30">
						请填写报名信息
					</view>
					<view class="u-font-24">
						为了更好地服务你，提交报名信息后即可免费领取课程
					</view>
				</view>
				<u-gap height="25"></u-gap>
				<view class="">
					<view class="cu-form-group">
						<view class="title">学习姓名</view>
						<input class="text-left" v-model="temp.name" placeholder="请输入(必填)" ></input>
					</view>
					<view class="cu-form-group">
						<view class="title">联系人手机</view>
						<input class="text-left" v-model="temp.phone" type="number" maxlength="11" placeholder="请输入(必填)" ></input>
					</view>
					<view class="cu-form-group" @click="show = true">
						<view class="title">学员生日</view>
						<input class="text-left" placeholder-class="placen" v-model="temp.birthday" disabled placeholder="请选择"  ></input>
						<view class="cuIcon-right"></view>
					</view>
					<view class="cu-form-group">
						<view class="title">学员年龄</view>
						<input class="text-left" placeholder-class="placen" v-model="temp.age" disabled placeholder="选择生日后自动计算"  ></input>
					</view>
					<view class="cu-form-group justify-start" style="border-bottom: 1px solid #eee;">
						<view class="title">学员性别</view>
						<view class="flex">
							<view class="">
								<text :class="temp.sex==1?'cuIcon-roundcheckfill text-orange':'cuIcon-round text-grey'" @click="xianbei(1)"></text>男
							</view>
							<view class="">
								<text :class="temp.sex==2?'cuIcon-roundcheckfill text-orange':'cuIcon-round text-grey'" @click="xianbei(2)"></text>女
							</view>
						</view>
					</view>
					<view class="flex u-font-24 shuming">
						提交信息即视为你已同意
						 <view class="text-blue"  @click="multiRedirect('/pages/web/web?url=web_protocol_url&type=authProtocol')" hover-class="none" style="font-size: 24rpx;">
							 个人信息使用授权书
						</view>
					</view>
					<button :loading="submitLoading" @click="handleSubmit" class="cu-btn u-font-30 bg-orange round">
						保存
					</button>
				</view>
			</view>
		</view>
		
		<u-picker v-model="show" mode="time" :end-year="endYear" @confirm="confirm"></u-picker>
		
		<view class="cu-modal share" :class="modalName=='Modal'?'show':''" @tap="hideModal">
				<view class="cu-dialog">
					<view class="baise">
						<image :src="share_img_url" mode="aspectFill"></image>
						<!-- <view class="top">
							<image :src="detail.cover_img" mode="aspectFill"></image>
							<view class="">
								<view class="name text-bold">
									{{detail.name}}
								</view>
								<view class="flex text-bold justify-between text-orange u-font-30">
									{{detail.free_intro}}
								</view>
							</view>
						</view>
						<view class="yitiaoxian">
							<image src="../../static/images/yitiaoxian.jpg" mode="widthFix"></image>
						</view>
						<view class="bottoms flex align-center justify-between">
							<view class="left flex align-center">
								<view class="img">
									<image :src="get_user_face" mode="aspectFill"></image>
								</view>
								<view class="">
									<view class="text-bold">
										{{vuex_user_name}}
									</view>
									<view class="">
										我的分享码：{{vuex_my_req_code}}
									</view>
								</view>
							</view>
							<view class="erwm">
								<image src="../../static/images/erwm.jpg" mode="aspectFit"></image> 
							</view>
						</view> -->
					</view>
					<view class="xiazhi" @click="handleDownFile">
						<image src="../../static/images/xiazhi.png" mode="aspectFit"></image>
						<view class="">
							<view class=""></view>
							 点击分享图片
						</view>
					</view>
				</view>
			
		</view>
		
		<u-popup v-model="popupLoading" mode="center" :mask-close-able="popupLoadingClose">
			<view style="text-align: center;margin-bottom: 20rpx;">
				<u-loading mode="circle" :show="popupLoading"></u-loading>
			</view>
			<text>{{popupLoadingTxt}}</text>
		</u-popup>
		
		
		
		
	</view>
</template>

<script>
	import {permission,permissionAuth} from "@/common/permission.js"
	export default {
		data() {
			const date = new Date()
			return {
				is_show_course:0,
				initLoading:false,
				popupLoadingClose:false,
				popupLoadingTxt:"",
				popupLoading:false,
				no_exist_msg:'',
				is_exist:0,
				buySubmitLoading:false,
				endYear:date.getFullYear(),
				submitLoading:false,
				detail:{},
				share_img_url:'',
				user_record:[],
				course_total:0,
				course_list:[],
				order_sort:'',
				
				background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
				autoplay: false,
				show: false,
				
				modalName: null,
				
				collect:false,//收藏
				
				list: [{
					name: '课程介绍'
				}],
				current: 0,

				content: '',
				style: {
					// 字符串的形式
					p: 'color: #333;font-size:30rpx;line-height: 44rpx;',
					span: 'font-size: 28rpx'
				},
				active:0,
				temp:{
					course_id:0,
					name:'',
					phone:'',
					birthday:'',
					sex:1,
				},
			}
		},
		computed:{
			get_img(){
				return this.detail.img||[]
			},
			get_free_state() {
				return this.detail.price-0 <= 0
			}
		},
		onLoad(options) {
			this.id = options.id
			this.temp.course_id = options.id
			this._initData()
			this.getCourseList()
		},
		methods: {
			_initData(){
				this.initLoading = false
				this.$u.http.post('/api/course/detail', {
					id:this.id,
					
				}).then(({ data }) => {
					this.detail = data.detail||{}
					this.user_record = data.user_record||[]
					this.is_exist = data.is_exist||0
					this.no_exist_msg = data.no_exist_msg||''
					this.share_img_url = data.share_img_url||''
					this.course_total = data.course_total||0
					this.collect = (data.is_coll||0)-0
					let is_show_course = data.is_show_course || 0
					this.is_show_course = 1
					if(is_show_course ==1){
						this.list = [{ name: '课程介绍' }, {name: '专栏目录' }];	
					}else{
						this.list = [{ name: '课程介绍' }];	
					}
					this.initLoading = true
				}).catch(()=>{
					this.initLoading = true
					
				})
			},
			getCourseList(){
				this.$u.http.post('/api/course/courseList', {
					id:this.id,
					order_sort:this.order_sort,
				}).then(({ data }) => {
					this.course_total = data.total||0
					this.course_list = data.list||[]
				}).catch(()=>{
					
					
				})
			},
			handleBuy(){
				this.submitLoading = true
				const that = this
				this.$u.http.post('/api/order/buyCourse', {
					id:this.id,
					openid:this.vuex_openid,
				}).then(({ data }) => {
					this.submitLoading = false
					
					let pay_info = data.info||{};
					let pay_obj = Object.assign({},pay_info,{
						success: function (res) {
							console.log('pay,..........success:' + JSON.stringify(res));
							that.$u.toast('支付成功')
							setTimeout(function(){
								uni.redirectTo({
									url:'/pages/orederList/orederList'
								})
							},1000)
							
						},
						fail: function (err) {
							console.log('fail:' + JSON.stringify(err));
							if(err.errMsg=='requestPayment:fail cancel'){
								that.$u.toast('已取消支付')
							}else{
								that.$u.toast('支付失败')
							}
						}})
					//唤醒支付
					console.log('pay_obj_______________',pay_obj)
					uni.requestPayment(pay_obj);
					
					
				}).catch(()=>{
					this.submitLoading = false
					
				})
			},
			handleDownItem(item){
				permissionAuth('scope.writePhotosAlbum').then((bool)=>{
					console.log('mmmmmmmmm',bool,item.file)
					this._downFile(item.file,(filePath)=>{
						const down_json = {
								filePath: filePath,
								success:  () =>{
										console.log('save success');
										this.$u.toast('文件保存成功')
								},
								fail:(err) =>{
										this.$u.toast('文件保存失败')
										console.log('打开文档成功',err);
								}
						};
						if(item.is_video){
								uni.saveVideoToPhotosAlbum(down_json)
						}else{
								uni.saveImageToPhotosAlbum(down_json);
						}
					})
				}).catch(e=>{
					console.log('eeeeeeeeee',e)
				})
				
			},
			handleDownFile(){
				
				permissionAuth('scope.writePhotosAlbum').then((bool)=>{
					console.log('mmmmmmmmm',bool)
					this._downFile(this.share_img_url,(filePath)=>{
						uni.saveImageToPhotosAlbum({
							filePath: filePath,
							success:  () =>{
								console.log('save success');
								this.$u.toast('文件保存成功')
							},
							fail:(err) =>{
								this.$u.toast('文件保存失败')
								console.log('打开文档成功',err);
							}
						});
					})
				}).catch(e=>{
					console.log('eeeeeeeeee',e)
				})
				
			},
			handleSort(){
				this.order_sort = this.order_sort=='desc'?'asc':'desc'
				this.getCourseList()
			},
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
			},
			hideModal(e) {
				this.modalName = null
			},
			Back() {
				uni.navigateBack({});
			},
			change(index) {
				this.current = index;
			},
			handleLearn(index) {
				this.current = 1;
			},
			handleLearnUnit(item){
				console.log(item)
				if(!this.is_exist){
					this.$u.toast(this.no_exist_msg)
					return;
				}
				if(item.is_video){
					
					this.$u.vuex('vuex_play_obj', item)
					this.$nextTick(function(){
						this.multiRedirect('/pages/web/web?url=web_show_url&course_item_id='+item.id)
						// this.multiRedirect('/pages/details/video')
					})
				}else{
					const down_file_path = item.file;
					// getApp().globalData.text = 'test'
					if(getApp().globalData.tempDownInfo[down_file_path]){
						uni.openDocument({
						  filePath: getApp().globalData.tempDownInfo[down_file_path],
						  success:  (res) => {
							console.log('打开文档成功');
						  }
						});
					}else{
						
						this._downFile(down_file_path,(filePath)=>{
							uni.openDocument({
							  filePath: filePath,
							  success:  (res) => {
								console.log('打开文档成功');
							  },
							  fail:(err) =>{
								   this.$u.toast('文件打开失败')
							  	console.log('打开文档成功',err);
							  }
							});
						})
						
					}
					
					
				}
				
			},
			_downFile(down_file_path,func){
				
				this.popupLoadingClose = false
				this.popupLoading = true
				this.popupLoadingTxt = "即将开始下载"
				const downloadTask = uni.downloadFile({
				  url: down_file_path,
				  success:  (res) => {
					this.popupLoadingTxt = "下载完成"
				    const filePath = res.tempFilePath;
					getApp().globalData.tempDownInfo[down_file_path] = filePath
					
					func(filePath)
				  },
				  fail:(err)=>{
					  this.popupLoadingTxt = "下载失败"
					  console.log(err)
					  this.$u.toast('文件下载失败')
				  },
				  complete:()=>{
					  this.popupLoadingClose = true
					  this.popupLoading = false
				  }
				});
				
				downloadTask.onProgressUpdate((res) => {
					this.popupLoadingTxt = "下载中("+res.progress+").."
				});
			},
			confirm(data){
				console.log(45454,data)
				this.temp.birthday = data.year+'-'+data.month+'-'+data.day
				this.temp.age = (new Date()).getFullYear()-data.year
			},
			xianbei(index){
				this.temp.sex = index
			},
			handleSubmit(){
				this.$u.http.post('/api/course/request', this.temp).then(({ msg,data }) => {
					this.$u.toast(msg)
				}).catch(()=>{
					
					
				})
			},
			djqd(){
				this.$u.http.post('/api/course/coll', {
					is_coll:this.collect ? 0:1,
					info:[
						{
							cond_id:this.id,
						}
					],
					
				}).then(({ msg,data }) => {
					this.$u.toast(msg)
					this.collect = 	data.state-0
				}).catch(()=>{
					
					
				})
			}
		}
	}
</script>

<style lang="scss">
	.swiper {
		position: relative;
	
		.index {
			position: absolute;
			padding: 5rpx 20rpx;
			border-radius: 50rpx;
			bottom: 10rpx;
			right: 10rpx;
			color: #FFFFFF;
			background-color: rgba(0, 0, 0, 0.2);
		}
		.banner {
			width: 100%;
			height: 100%;
			image {
				border-radius: 10rpx;
				width: 100%;
				height: 100%;
			}
		}
	}
	.slot{
		margin: 0 10rpx;
		width: 180rpx;
		height: 60rpx;
		border: 2rpx solid #F1F1F1;
		border-radius: 50rpx;
		color: #F1F1F1;
		.cuIcon-back{
			font-size: 40rpx;
			color: #555555;
			margin-right: 20rpx;
		}
		.cuIcon-home{
			font-size: 44rpx;
			color: #555555;
			margin-left: 18rpx;
		}
	}
	.main {
		padding: 35rpx 30rpx 20rpx;
		image {
			width: 690rpx;
			height: 409rpx;
			border-radius: 10rpx;
		}

		.name {
			font-size: 40rpx;
			color: #333333;
		}

		.text {
			margin-top: 30rpx;
			border-bottom: 2rpx solid #f2f2f2;

			.u-font-30 {
				color: #333;
				margin-right: 10rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
			}

			.u-font-24 {
				color: #999;
				margin: 30rpx 0;
			}

		}

		.fenxian {
			margin-top: 20rpx;

			image {
				width: 48rpx;
				height: 46rpx;
			}
		}

		.cu-btn {
			width: 113rpx;
			height: 60rpx;
			font-size: 24rpx;
		}

		.img {
			.u-font-30 {
				padding: 35rpx 0 20rpx;
				color: #666;
			}
			.u-flex{
				position: relative;
				.avatar{
					image{
						position: relative;
					}
					image:nth-child(1){
						left: 0rpx;
						z-index: 10;
					}
					image:nth-child(2){
						left: -20rpx;
						z-index: 9;
					}
					image:nth-child(3){
						left: -40rpx;
						z-index: 8;
					}
					image:nth-child(4){
						left: -60rpx;
						z-index: 7;
					}
					image:nth-child(5){
						left: -80rpx;
						z-index: 6;
					}
					image:nth-child(6){
						left: -100rpx;
						z-index: 5;
					}
				}
				image {
					width: 70rpx;
					height: 70rpx;
					border-radius: 50%;
				}
			}
		}
	}

	.tabs {
		padding: 20rpx 0;
	}
	.u-content{
		padding: 40rpx 30rpx 25rpx;
	}
	.bottom {
		width: 100%;
		position: fixed;
		bottom: 0;
		border-top: 2rpx solid #E8E8E8;
		padding: 15rpx 0;
		background-color: #FFFFFF;
		.cu-btn {
			margin-left: 60rpx;
			width: 520rpx;
			height: 82rpx;
		}
	}
	.nulu{
		.shiping{
			margin-top: 50rpx;
			image{
				width: 45rpx;
				height: 45rpx;
			}
		}
		image{
			width: 84rpx;
			height: 30rpx;
		}
		.cu-btn{
			padding: 0 18rpx;
			height: 42rpx;
			margin-right: 20rpx;
			color: #666;
		}
		.u-font-24{
			margin-top: 15rpx;
			color: #999;
		}
	}
	.cu-modal.bottom-modal{
		.cu-dialog{
			border-radius: 50rpx 50rpx 0 0;
			position: relative;
			background-color: #FFFFFF;
			.action{
				position: absolute;
				right: 30rpx;
				top: 28rpx;
				color: #C6C7CA;
			}
			.u-font-30{
				padding: 24rpx 0;
			}
			.u-font-24{
				color: #999;
			}
			.title{
				width: 170rpx;
			}
			.title,.text-left{
				font-size: 26rpx;
			}
			.placen{
				color: #999;
			}
			.justify-start{
				.flex{
					>view{
						margin-right: 80rpx;
					}
				}
				text{
					font-size: 36rpx;
					padding: 0 10rpx;
				}
			}
			.shuming{
				padding: 25rpx;
			}
			.cu-btn{
				margin: 10rpx 0 24rpx;
				width: 520rpx;
				height: 82rpx;
			}
		}
	}
	.share{
		.cu-dialog{
			background-color: transparent;
			text-align: left;
			width: 520rpx;
			border-radius: 0;
			.baise{
				background-color: #FFFFFF;
				width: 518rpx;
				height: 812rpx;
				image{
					width: 518rpx;
					height: 812rpx;
				}
			}
			.top{
				padding: 35rpx 35rpx 0;
				image{
					width: 446rpx ;
					height: 446rpx;
					border-radius: 8rpx;
				}
				.name{
					margin-top: 50rpx;
					font-size: 32rpx;
				}
				.flex{
					padding:  16rpx 0;
					text{
						font-size: 30rpx;
					}
				}
			}
			.yitiaoxian{
				width: 100%;
			}
			.bottoms{
				padding: 20rpx 35rpx;
				.left{
					font-size: 24rpx;
					.img{
						margin-right: 20rpx;
					}
					
					image{
						width: 70rpx ;
						height: 70rpx;
						border-radius: 50%;
					}
				}
				.erwm{
					image{
						width: 120rpx ;
						height: 120rpx;
					}
				}
			}
			.xiazhi{
				text-align: center;
				image{
					width: 65rpx ;
					height: 65rpx;
					margin: 10rpx 0;
				}
				font-size: 28rpx;
				color: #FFFFFF;
			}
		}
	}
</style>
